﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using CurrencyConverter;
using System.Threading;
using System.Net;

namespace CurrencyConverterDemo
{
    public partial class CurrencyForm : Form
    {
        Converter Converter = new Converter();

       
         
            
        public CurrencyForm()
        {
            //Converter.
            Converter.OnCompleted += new OperationCompletedEventHandler(Converter_OnCompleted);
            InitializeComponent();
        }

        private void CurrencyForm_Load(object sender, EventArgs e)
        {
            FromCurrency.DataSource    = Converter.GetDataSet().Tables["Currency"];
            FromCurrency.DisplayMember = "Description";
            FromCurrency.ValueMember   = "code";

            ToCurrency.DataSource     = Converter.GetDataSet().Tables["Currency"];
            ToCurrency.DisplayMember  = "Description";
            ToCurrency.ValueMember    = "code";
                        
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Converter.From = (Currencies)Enum.Parse(typeof(Currencies), FromCurrency.SelectedValue.ToString());
            Converter.To = (Currencies)Enum.Parse(typeof(Currencies), ToCurrency.SelectedValue.ToString());
            Converter.amount = Convert.ToDouble(Amount.Text);

            int OldFromSlected         = FromCurrency.SelectedIndex;
            FromCurrency.SelectedIndex = ToCurrency.SelectedIndex;
            ToCurrency.SelectedIndex   = OldFromSlected;
          
            Converter.Reverse();
            Converter.Result(Converter.ResultFormatting.ConvertedValueAndFromAmount);

     
            




        }

        private void button1_Click(object sender, EventArgs e)
        {

            if (Amount.Text == "")
                return;
            Converter.From   =    (Currencies)Enum.Parse(typeof(Currencies), FromCurrency.SelectedValue.ToString());
            Converter.To     =    (Currencies)Enum.Parse(typeof(Currencies), ToCurrency.SelectedValue.ToString());
            Converter.amount =    Convert.ToDouble(Amount.Text);

            Converter.Result(Converter.ResultFormatting.OnlyConvertedValue);
           

         
        }

  

        void Converter_OnCompleted(object sender, OperationCompletedEventArgs e)
        {
            LabelResult.Text = "";
            if (e.ListResult != null) // If You Need a result with all Formatting Options
            {
             
                foreach (string Str in e.ListResult)
                {
                    LabelResult.Text += Str + "\r\n";
                
                }

                return;

            }
   
            label1.Text = e.Result; // Only One Formatting Option


           

        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (Amount.Text == "")
                return;

            Converter.From = (Currencies)Enum.Parse(typeof(Currencies), FromCurrency.SelectedValue.ToString());
            Converter.To = (Currencies)Enum.Parse(typeof(Currencies), ToCurrency.SelectedValue.ToString());
            Converter.amount = Convert.ToDouble(Amount.Text);

            Converter.Result(Converter.ResultFormatting.ConvertedValueDescription);
           

  
        }

        /// <summary>
        /// ConvertedValueAndFromAmount Button
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button4_Click(object sender, EventArgs e)
        {
            if (Amount.Text == "")
                return;

            Converter.From = (Currencies)Enum.Parse(typeof(Currencies), FromCurrency.SelectedValue.ToString());
            Converter.To = (Currencies)Enum.Parse(typeof(Currencies), ToCurrency.SelectedValue.ToString());
            Converter.amount = Convert.ToDouble(Amount.Text);

            Converter.Result(Converter.ResultFormatting.ConvertedValueAndFromAmount); // 100 USD = 70.54 EUR
          


        }

        private void button5_Click(object sender, EventArgs e)
        {
            if (Amount.Text == "")
                return;

            Converter.From = (Currencies)Enum.Parse(typeof(Currencies), FromCurrency.SelectedValue.ToString());
            Converter.To = (Currencies)Enum.Parse(typeof(Currencies), ToCurrency.SelectedValue.ToString());
            Converter.amount = Convert.ToDouble(Amount.Text);

            Converter.Result(Converter.ResultFormatting.All);



        }


        
    }
}
